﻿package mortal.game.view.common.item
{
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.resource.info.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import com.mui.manager.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.common.cd.effect.*;
    import mortal.mvc.core.*;

    public class CDItem extends GSprite implements IDragDrop, IToolTipItem
    {
        protected var _pos:int;
        public var dropChecktFunction:Function;
        protected var _freezingEffect:CDFreezingEffect;
        protected var _isShowFreezingEffect:Boolean = false;
        protected var _leftTimeEffect:CDLeftTimeEffect;
        protected var _isShowLeftTimeEffect:Boolean = false;
        protected var _cdData:ICDData;
        protected var _myData:Object;
        protected var _dragAble:Boolean = false;
        protected var _dropAble:Boolean = false;
        protected var _throwAble:Boolean = true;
        protected var _isShowToolTip:Boolean = true;
        protected var _isRegistedToolTip:Boolean = false;
        protected var _bitmapdata:BitmapData = null;
        protected var _bitmap:GBitmap = null;
        protected var imageUrl:String = "";
        protected var _bg:ScaleBitmap;
        protected var _bgName:String;
        protected var _bottomLayer:GSprite;
        protected var _middleLayer:GSprite;
        protected var _topLayer:GSprite;
        protected var _dragSource:Object;
        protected var _paddingTop:int = 2;
        protected var _paddingLeft:int = 2;
        private var _reactionTime:int = 500;
        protected var _tooltipData:Object;
        private var _lastImageUrl:String;
        private var _enabled:Boolean;

        public function CDItem()
        {
            _width = 32;
            _height = 32;
            return;
        }// end function

        override protected function configUI() : void
        {
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStage);
            this.addEventListener(DragEvent.Event_Start_Drag, this.startDragHandler);
            super.configUI();
            return;
        }// end function

        private function startDragHandler(event:DragEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.StartDragDragables, event.dragSouce));
            this.addEventListener(DragEvent.Event_Stop_Drag, this.stopDragHandler);
            return;
        }// end function

        private function stopDragHandler(event:DragEvent) : void
        {
            this.removeEventListener(DragEvent.Event_Stop_Drag, this.stopDragHandler);
            Dispatcher.dispatchEvent(new DataEvent(EventName.StopDragDragables, event.dragSouce));
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseChildren = false;
            this.mouseEnabled = true;
            this._bottomLayer = UIFactory.getUICompoment(GSprite, 0, 0, this);
            this._middleLayer = UIFactory.getUICompoment(GSprite, 0, 0, this);
            this._topLayer = UIFactory.getUICompoment(GSprite, 0, 0, this);
            setSize(40, 40);
            this._bitmap = UIFactory.bitmap("", this._paddingLeft, this._paddingTop, this._bottomLayer);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._reactionTime = 500;
            this.source = "";
            this._bottomLayer.dispose(param1);
            this._bottomLayer = null;
            this._middleLayer.dispose(param1);
            this._middleLayer = null;
            this._topLayer.dispose(param1);
            this._topLayer = null;
            if (this._bg)
            {
                this._bg.dispose(param1);
                this._bg = null;
            }
            UIFactory.disposeGBitmap(this._bitmap);
            this._bitmap = null;
            this.dropChecktFunction = null;
            this.clearAllEffect();
            this._bitmapdata = null;
            this._bgName = null;
            this._lastImageUrl = "";
            this._tooltipData = "";
            this._dragAble = false;
            this._dropAble = false;
            this._throwAble = true;
            this._isShowToolTip = true;
            this._isRegistedToolTip = false;
            this._paddingTop = 2;
            this._paddingLeft = 2;
            this._isShowFreezingEffect = false;
            this._isShowLeftTimeEffect = false;
            this._isShowToolTip = true;
            this._isRegistedToolTip = false;
            return;
        }// end function

        public function get pos() : int
        {
            return this._pos;
        }// end function

        public function set pos(param1:int) : void
        {
            this._pos = param1;
            return;
        }// end function

        protected function onAddToStage(event:Event) : void
        {
            this.registerTips();
            this.registerEffects();
            return;
        }// end function

        protected function onRemoveFromStage(event:Event) : void
        {
            this.unRegisterTips();
            this.unRegisterEffects();
            return;
        }// end function

        protected function judgeToolTip(event:Event = null) : void
        {
            if (Global.stage.contains(this))
            {
                return;
            }
            if (event && event.type == Event.ADDED_TO_STAGE && this.toolTipData || !event && this.toolTipData && Global.stage.contains(this))
            {
                ToolTipsManager.register(this);
            }
            else
            {
                ToolTipsManager.unregister(this);
            }
            return;
        }// end function

        public function updateCDEffect(param1:Object = null, param2:int = 2) : void
        {
            this.unRegisterEffects();
            if (param1 == null)
            {
                this._cdData = null;
                return;
            }
            this._cdData = Cache.instance.cd.getCDData(param1, param2);
            this.registerEffects();
            return;
        }// end function

        public function get reactionTime() : int
        {
            return this._reactionTime;
        }// end function

        public function set reactionTime(param1:int) : void
        {
            this._reactionTime = param1;
            return;
        }// end function

        public function get isCDing() : Boolean
        {
            if (this._cdData == null)
            {
                return false;
            }
            return this._cdData.isCoolDown;
        }// end function

        public function get toolTipData()
        {
            return this._tooltipData;
        }// end function

        public function set toolTipData(param1) : void
        {
            this._tooltipData = param1;
            this.judgeToolTip();
            return;
        }// end function

        public function set isShowToolTip(param1:Boolean) : void
        {
            this._isShowToolTip = param1;
            if (!this._isShowToolTip)
            {
                this.unRegisterTips();
            }
            else
            {
                this.registerTips();
            }
            return;
        }// end function

        protected function registerTips() : void
        {
            if (this._isShowToolTip && !this._isRegistedToolTip)
            {
                ToolTipsManager.register(this);
                this._isRegistedToolTip = true;
            }
            return;
        }// end function

        protected function unRegisterTips() : void
        {
            if (this._isRegistedToolTip)
            {
                ToolTipsManager.unregister(this);
                this._isRegistedToolTip = false;
            }
            return;
        }// end function

        override protected function updateView() : void
        {
            super.updateView();
            this.resetBitmapSize();
            if (this._bg)
            {
                this._bg.width = _width;
                this._bg.height = _height;
            }
            this.resetEffectPlace();
            return;
        }// end function

        protected function resetEffectPlace() : void
        {
            if (this._freezingEffect)
            {
                this._freezingEffect.x = _width / 2;
                this._freezingEffect.y = _height / 2;
                this._freezingEffect.setMaskSize(_width - this._paddingLeft * 2, _height - this._paddingTop * 2);
            }
            if (this._leftTimeEffect)
            {
                this._leftTimeEffect.width = _width;
                this._leftTimeEffect.height = 20;
                this._leftTimeEffect.x = 0;
                this._leftTimeEffect.y = (_height - this._leftTimeEffect.height) / 2;
            }
            return;
        }// end function

        public function get dragSource() : Object
        {
            return this._dragSource;
        }// end function

        public function set dragSource(param1:Object) : void
        {
            this._dragSource = param1;
            return;
        }// end function

        public function get isDropAble() : Boolean
        {
            return this._dropAble;
        }// end function

        public function set isDropAble(param1:Boolean) : void
        {
            this._dropAble = param1;
            return;
        }// end function

        public function get isDragAble() : Boolean
        {
            return this._dragAble;
        }// end function

        public function set isDragAble(param1:Boolean) : void
        {
            this._dragAble = param1;
            if (this._dragAble)
            {
                this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            else if (this.hasEventListener(MouseEvent.MOUSE_DOWN))
            {
                this.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
            }
            return;
        }// end function

        protected function onMouseDown(event:MouseEvent) : void
        {
            if (this._dragAble)
            {
                DragManager.instance.startDragItem(this, this.bitmapdata);
            }
            return;
        }// end function

        public function get isThrowAble() : Boolean
        {
            return this._throwAble;
        }// end function

        public function set isThrowAble(param1:Boolean) : void
        {
            this._throwAble = param1;
            return;
        }// end function

        public function canDrop(param1:IDragDrop, param2:IDragDrop) : Boolean
        {
            if (this.dropChecktFunction != null)
            {
                return this.dropChecktFunction(param1, this);
            }
            return this.isDropAble;
        }// end function

        public function get bitmapdata() : BitmapData
        {
            var scal:Number;
            if (isDisposed)
            {
                return null;
            }
            try
            {
                if (this._bitmapdata)
                {
                    if (this._bitmapdata.width > this.width + 5 || this._bitmapdata.height > this.height + 5)
                    {
                        scal = this.width / this._bitmapdata.width;
                        this._bitmapdata = new BitmapData(this.width, this.height, true, 0);
                        this._bitmapdata.draw(this._bitmap, new Matrix(scal, 0, 0, scal, 0, 0));
                    }
                }
                return this._bitmapdata;
            }
            catch (e:Error)
            {
                if (Global.isDebugModle)
                {
                    throw e;
                }
            }
            return null;
        }// end function

        public function set source(param1:Object) : void
        {
            GImageBitmap.removeReference(this.imageUrl);
            if (this._lastImageUrl)
            {
                LoaderManager.instance.removeResourceEvent(this._lastImageUrl, this.onLoadCompleteHandler);
                this._lastImageUrl = "";
            }
            if (param1 is Bitmap)
            {
                this._bitmapdata = (param1 as Bitmap).bitmapData;
                this._bitmap.bitmapData = this._bitmapdata;
                this._bitmap.smoothing = true;
                this.resetBitmapSize();
                return;
            }
            if (param1 is BitmapData)
            {
                this._bitmapdata = param1 as BitmapData;
                this._bitmap.bitmapData = this._bitmapdata;
                this._bitmap.smoothing = true;
                this.resetBitmapSize();
                return;
            }
            this._bitmap.bitmapData = null;
            if (param1 is String && param1 != "")
            {
                this.imageUrl = param1 as String;
                this._lastImageUrl = this.imageUrl;
                GImageBitmap.addReference(this.imageUrl);
                LoaderManager.instance.load(this.imageUrl, this.onLoadCompleteHandler, LoaderPriority.LevelB);
            }
            else if (this.imageUrl)
            {
                this.imageUrl = null;
            }
            return;
        }// end function

        protected function onLoadCompleteHandler(param1:ImageInfo) : void
        {
            this._lastImageUrl = "";
            this._bitmapdata = param1.bitmapData;
            this._bitmap.bitmapData = this._bitmapdata;
            this._bitmap.smoothing = true;
            this.resetBitmapSize();
            return;
        }// end function

        public function set bgName(param1:String) : void
        {
            if (this._bgName == param1)
            {
                return;
            }
            this._bgName = param1;
            if (param1)
            {
                if (this._bg)
                {
                    this._bg.bitmapData = GlobalClass.getBitmapData(param1);
                    this._bg.scale9Grid = ResourceConst.getRectangle(param1);
                }
                else
                {
                    this._bg = GlobalClass.getScaleBitmap(param1, ResourceConst.getRectangle(param1));
                    this._bottomLayer.addChildAt(this._bg, 0);
                }
            }
            else if (this._bg)
            {
                this._bg.dispose(true);
                this._bg = null;
            }
            return;
        }// end function

        protected function resetBitmapSize() : void
        {
            if (this._bitmap)
            {
                this._bitmap.width = _width - this._paddingLeft * 2;
                this._bitmap.height = _height - this._paddingTop * 2;
            }
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.filters = [];
            this.mouseEnabled = true;
            super.dispose(param1);
            return;
        }// end function

        protected function registerEffects() : void
        {
            if (this._cdData == null)
            {
                return;
            }
            this.registerFreezingEffect();
            this.registerLeftTimeEffect();
            this._cdData.addFinishCallback(this.cdFinishedHandler);
            return;
        }// end function

        protected function registerFreezingEffect() : void
        {
            if (this._cdData == null)
            {
                return;
            }
            if (this._isShowFreezingEffect)
            {
                if (this._freezingEffect == null)
                {
                    this._freezingEffect = new CDFreezingEffect();
                    this.resetEffectPlace();
                }
                if (this._freezingEffect.parent == null)
                {
                    this._topLayer.addChild(this._freezingEffect);
                }
                if (!this._freezingEffect.registed)
                {
                    this._cdData.addEffect(this._freezingEffect);
                }
            }
            return;
        }// end function

        protected function registerLeftTimeEffect() : void
        {
            var _loc_1:TextFormat = null;
            if (this._cdData == null)
            {
                return;
            }
            if (this._isShowLeftTimeEffect)
            {
                if (this._leftTimeEffect == null)
                {
                    this._leftTimeEffect = new CDLeftTimeEffect();
                    this._leftTimeEffect.filters = [FilterConst.glowFilter];
                    _loc_1 = GlobalStyle.textFormatHuang;
                    _loc_1.align = TextFormatAlign.CENTER;
                    _loc_1.size = 13;
                    this._leftTimeEffect.defaultTextFormat = _loc_1;
                    this.resetEffectPlace();
                }
                if (this._leftTimeEffect.parent == null)
                {
                    this._topLayer.addChild(this._leftTimeEffect);
                }
                if (!this._leftTimeEffect.registed)
                {
                    this._cdData.addEffect(this._leftTimeEffect);
                }
            }
            return;
        }// end function

        protected function unRegisterEffects() : void
        {
            if (this._cdData == null)
            {
                return;
            }
            this.unRegisterFreezingEffect();
            this.unRegisterLeftTimeEffect();
            this._cdData.removeFinishCallback(this.cdFinishedHandler);
            return;
        }// end function

        protected function unRegisterFreezingEffect() : void
        {
            if (this._freezingEffect && this._freezingEffect.registed)
            {
                this._freezingEffect.reset();
                this._cdData.removeEffect(this._freezingEffect);
                DisplayUtil.removeMe(this._freezingEffect);
            }
            return;
        }// end function

        protected function unRegisterLeftTimeEffect() : void
        {
            if (this._leftTimeEffect && this._leftTimeEffect.registed)
            {
                this._leftTimeEffect.reset();
                this._cdData.removeEffect(this._leftTimeEffect);
                DisplayUtil.removeMe(this._leftTimeEffect);
            }
            return;
        }// end function

        protected function cdFinishedHandler() : void
        {
            return;
        }// end function

        protected function clearAllEffect() : void
        {
            if (this._cdData)
            {
                if (this._freezingEffect)
                {
                    this._cdData.removeEffect(this._freezingEffect);
                    DisplayUtil.removeMe(this._freezingEffect);
                    this._freezingEffect = null;
                }
                if (this._leftTimeEffect)
                {
                    this._cdData.removeEffect(this._leftTimeEffect);
                    DisplayUtil.removeMe(this._leftTimeEffect);
                    this._leftTimeEffect = null;
                }
                this._cdData = null;
            }
            this.unRegisterTips();
            this._isShowFreezingEffect = false;
            this._isShowLeftTimeEffect = false;
            return;
        }// end function

        public function set enabled(param1:Boolean) : void
        {
            this._enabled = param1;
            if (!param1)
            {
                this.filters = [FilterConst.colorFilter];
            }
            else
            {
                this.filters = [];
            }
            return;
        }// end function

        public function set bitmapEnable(param1:Boolean) : void
        {
            if (this.bitmap)
            {
                if (!param1)
                {
                    this.bitmap.filters = [FilterConst.colorFilter];
                }
                else
                {
                    this.bitmap.filters = [];
                }
            }
            return;
        }// end function

        public function get enabled() : Boolean
        {
            return this._enabled;
        }// end function

        public function get isShowLeftTimeEffect() : Boolean
        {
            return this._isShowLeftTimeEffect;
        }// end function

        public function set isShowLeftTimeEffect(param1:Boolean) : void
        {
            if (param1 == this._isShowLeftTimeEffect)
            {
                return;
            }
            this._isShowLeftTimeEffect = param1;
            if (!param1)
            {
                this.unRegisterLeftTimeEffect();
            }
            else if (param1)
            {
                this.registerLeftTimeEffect();
            }
            return;
        }// end function

        public function get bitmap() : Bitmap
        {
            return this._bitmap;
        }// end function

        public function get isShowFreezingEffect() : Boolean
        {
            return this._isShowFreezingEffect;
        }// end function

        public function set isShowFreezingEffect(param1:Boolean) : void
        {
            if (this._isShowFreezingEffect == param1)
            {
                return;
            }
            this._isShowFreezingEffect = param1;
            if (!param1)
            {
                this.unRegisterFreezingEffect();
            }
            else
            {
                this.registerFreezingEffect();
            }
            return;
        }// end function

    }
}
